<template>
  <div class="OrganizationIndex">
    <jxtech-basic-header>
      <template #extra>
        <!-- <jxtech-add-button @click='RulelibraryState.addhandler'></jxtech-add-button>
        <jxtech-delete-button @click='RulelibraryState.deletehandler'></jxtech-delete-button> -->
      </template>
    </jxtech-basic-header>

    <el-container>
      <el-aside :width="`${OrganizationState.asideWidth}px`" class="power-aside">
        <el-input v-model="OrganizationState.filterText" placeholder="按名称进行搜索" clearable />
        <el-tree class="filter-tree" ref="treeRef" node-key="mrCode" :default-expand-all="true" :props="OrganizationState.defaultProps"
                 :data="OrganizationState.treeData" :highlight-current="true" :expand-on-click-node="false"
                 :filter-node-method="OrganizationState.filterNode" @node-click="OrganizationState.TreeNodeClick">
          <template #default="{ node, data }">
            <span class="custom-tree-node">
              <span :title="node.label" class="label">
                <jxtech-svg-icon :icon="'#icon-detail'" :size="16" class="pointer" v-if="data.orgLevel == 1"></jxtech-svg-icon>
                <jxtech-svg-icon :icon="'#icon-attachent'" :size="16" class="pointer" v-else-if="data.orgLevel == 2"></jxtech-svg-icon>
                <jxtech-svg-icon :icon="'#icon-modular'" :size="16" class="pointer" v-else></jxtech-svg-icon>
                {{ node.label }}
              </span>
            </span>
          </template>
        </el-tree>
        <dragResize v-model:asideWidth="OrganizationState.asideWidth" v-model:dragIng="OrganizationState.dragIng"></dragResize>
      </el-aside>

      <el-main style="padding: 0;">
        <organization-form-com ref="OrganizationFormComRef" :ParentQueryParam="OrganizationState.queryParam"></organization-form-com>
      </el-main>
    </el-container>
  </div>
</template>

<script lang="ts" setup>
import { reactive, watch, onMounted, ref, nextTick } from "vue"
import dragResize from "/@/views/base/power-station-power-info/component/drag-resize.vue"
import OrganizationFormCom from "./component/organization-form.vue"
import { getOrgTreeServiceApi } from "/@/api/personnel/bean/staffinfo"
const treeRef = ref()
const OrganizationFormComRef = ref()
const OrganizationState = reactive({
  asideWidth: 300,
  dragIng: false,
  isStation: false,
  queryParam: { mrCode: "MR_00" },
  filterText: "",
  treeData: [],
  defaultProps: {
    children: "mdmOrganizations",
    label: "orgName"
  },
  initTreeData: () => {
    getOrgTreeServiceApi.getOrgTree(null, null).then((response: any) => {
      OrganizationState.treeData = response.data.aa
    })
  },
  filterNode(value: any, data: any) {
    if (!value) return true
    return data.orgName.indexOf(value) !== -1
  },
  TreeNodeClick(data: any, node: any, obj: any) {
    // console.log(data.mrCode)
    OrganizationState.queryParam.mrCode = data.mrCode
    nextTick(() => OrganizationFormComRef.value.exposeinitgetOrgInfo())
  }
})
watch(
  () => OrganizationState.filterText,
  val => {
    treeRef.value!.filter(val)
  }
)
onMounted(() => {
  OrganizationState.initTreeData()
})
</script>

<style lang="scss" scoped>
.OrganizationIndex {
  height: 100%;
  .el-container {
    height: calc(100% - 45px);
  }

  .el-main {
    padding: 0;
    background-color: #fff;
    overflow-y: scroll;
  }
}
</style>
